﻿using HAF.NetUtility.Assistantor;
using Iptv.ResourceGather.Model;
using RDS.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;

namespace Iptv.ResourceGather.SubItems
{
    /// <summary>
    /// 媒资数据采集工作项
    /// </summary>
    public class GatherMediaItem : PluginWorkItem
    {
        GatherRuleConfig gatherCSPRule;
        /// <summary>
        /// 执行工作项
        /// </summary>
        public override void ExecItem()
        {
            base.ExecItem();

            if (WorkItems is GatherRuleConfig)
            {
                gatherCSPRule = WorkItems as GatherRuleConfig;
            }
            else
            {
                return;
            }

            //读取配置
            //RefreshConfig();
            //提取请求方输入参数
            Thread uploadMonitor = new Thread(delegate () { MediaGatherTask(this.WorkJson); }) { Name = "GatherTaskMonitor", IsBackground = true, Priority = ThreadPriority.Highest };
            uploadMonitor.Start();
        }

        private void MediaGatherTask(string gatherPath)
        {
            //检查目录是否存在
            if (Directory.Exists(gatherPath))
            {
                //获取目录下所有文件（包括子目录）
                List<string> tmpFiles = Directory.EnumerateFiles(gatherPath, "*.*", SearchOption.AllDirectories).ToList<string>();
                if (tmpFiles != null && tmpFiles.Count > 0)
                {
                    ShowDebugMessage("目录 {0} 下搜寻到文件{1}个文件", gatherPath, tmpFiles.Count);
                    foreach (var item in tmpFiles)
                    {
                        FileInfo tmpfile = new FileInfo(item);
                        if (gatherCSPRule.AllowExpiredTransfer)
                        {
                            WriteWarnMessage("对文件[{0}]启动过期校验", item);
                            if (tmpfile.CreationTime.AddDays(gatherCSPRule.ExpiredTime) >= DateTime.Now)
                            {
                                WriteInfoMessage("文件[{0}]建档日期为{1}，判定为[未过期]", tmpfile.Name, tmpfile.CreationTime);
                            }
                            else
                            {
                                string transferFile = Path.Combine(gatherCSPRule.RecyclebinRoot, DateTime.Now.ToString("yyyyMM/dd").Replace("/", "\\"), tmpfile.Name);
                                WriteWarnMessage("文件[{0}]建档时间为{1}，判定为[已过期]，将被迁移到{2}", tmpfile.Name, tmpfile.CreationTime, transferFile);
                                //文件迁移
                                if (gatherCSPRule.Code.ToUpper() == "CGN" || gatherCSPRule.Code.ToUpper() == "MRS_DSP" || gatherCSPRule.Code.ToUpper() == "MRS_ZZN" 
                                    || gatherCSPRule.Code.ToUpper() == "MRS_DXJD" || gatherCSPRule.Code.ToUpper() == "MRS_XMLBMF" || gatherCSPRule.Code.ToUpper() == "MRS_XMLBFF" 
                                    || gatherCSPRule.Code.ToUpper() == "MRS_QCZJ" || gatherCSPRule.Code.ToUpper() == "MRS_CMSGAME" || gatherCSPRule.Code.ToUpper() == "MRS_ZBZQ")
                                {
                                    FileAssistantor.FileMove(item, transferFile, WorkID);
                                }
                            }
                        }
                        if (tmpfile.Extension == ".xml")
                        {
                            WriteWarnMessage("这是一个XML文件");
                        }
                        //WriteDebugMessage("文件{0}创建时间为{1}", item, tmpfile.CreationTime);
                    }
                }
                else
                {
                    ShowWarnMessage("目录 {0} 下未搜寻到文件", gatherPath);
                }
            }
            else
            {
                ShowDebugMessage("目录 {0} 不存在", gatherPath);
            }
            
        }
    }
}
